<template>
	<view class="normal-login-container" :style="{backgroundImage: 'url('+(themeData.logiMobileUrl ? themeData.logiMobileUrl.url : '')+')'}">
		<view class="logo-content">
			<image style="width: 100px;height: 100px;border-radius: 50%;" :src="themeData.logoUrl ? themeData.logoUrl.url : '../../static/logo.png'" mode="widthFix">
			</image>
			<br />
			<br />
			<text class="title">{{themeData.mobileLoginBackgroundName ? themeData.mobileLoginBackgroundName : '运油宝-管控'}}</text>
		</view>
		<view class="login-form-content">
			<view class="input-item" v-for="(placeholder, key) in inputFields" :key="key" style="display: flex;justify-content: center;align-items: center;">
				<view class="icon">
					<uni-icons :type="icons[key]" size="19"></uni-icons>
				</view>
				<input v-model="loginForm[key]" class="input" :type="key === 'password' ? 'password' : 'text'" :placeholder="placeholder" :maxlength="maxLength[key]" />
			</view>
			<view class="input-item" style="width: 60%;margin: 0px;display: flex;justify-content: center;align-items: center;" v-if="captchaEnabled">
				<view class="icon">
					<uni-icons type="checkbox" size="19"></uni-icons>
				</view>
				<input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
				<view style="height: 38px;float: right;">
					<image :src="codeUrl" @click="getCode" style="height: 38px;position: absolute;margin-left: 10px;width: 104px;"></image>
				</view>
			</view>
			<view style="display: flex;justify-content: space-between;padding-top: 10px;">
				<span>
					<text>没有账号？</text>
					<text @click="handleUserRegister" style="color: blue;">立即注册</text>
				</span>
				<text @click="handleUserFindPassword">找回密码</text>
			</view>
			<view class="action-btn">
				<button @click="handleLogin" hover-class="btnHover" class="login-btn">登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import { getCodeImg } from '@/api/login'

	export default {
		data() {
			const themeData = JSON.parse(uni.getStorageSync('info') || '{}');
			return {
				codeUrl: "",
				captchaEnabled: true,
				register: false,
				loginForm: {
					dept: "",
					phone: "",
					password: "",
					code: "",
					uuid: ""
				},
				themeData,
				inputFields: {
					dept: "服务商名称/服务商ID",
					phone: "请输入手机号",
					password: "请输入密码"
				},
				icons: {
					dept: "flag",
					phone: "person",
					password: "locked"
				},
				maxLength: {
					dept: 12,
					phone: 11,
					password: 20
				}
			}
		},
		created() {
			this.getCode()
		},
		methods: {
			getCode() {
				getCodeImg().then(res => {
					this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled
					if (this.captchaEnabled) {
						this.codeUrl = 'data:image/gif;base64,' + res.data.img
						this.loginForm.uuid = res.data.uuid
					}
				})
			},
			handleUserRegister() {
				uni.navigateTo({
					url: '/pages/login/register'
				})
			},
			handleUserFindPassword() {
				uni.navigateTo({
					url: '/pages/login/findPassword'
				})
			},
			async handleLogin() {
				if (this.loginForm.dept === "") {
					uni.showToast({
						icon: 'error',
						title: '请输入您的组织'
					})
				} else if (this.loginForm.phone === "") {
					uni.showToast({
						icon: 'error',
						title: '请输入您的账号'
					})
				} else if (this.loginForm.password === "") {
					uni.showToast({
						icon: 'error',
						title: '请输入您的密码'
					})
				} else {
					uni.showLoading({
						mask: true,
						title: '登录中...'
					})
					this.pwdLogin()
				}
			},
			async pwdLogin() {
				this.$store.dispatch('Login', this.loginForm).then((res) => {
					uni.hideLoading()
					this.loginSuccess()
				}).catch(error => {
					if (this.captchaEnabled) {
						this.getCode()
					}
				})
			},
			loginSuccess() {
				this.$store.dispatch('GetInfo').then(res => {
					if (res.data.user.dept.status === '1') {
						uni.navigateTo({
							url: '/pages/login/authentication?deptId=' + res.data.user.deptId
						})
					} else if (res.data.user.status === '1') {
						uni.showToast({
							icon: 'none',
							title: '当前用户账号被停用，无法登录'
						})
					} else {
						uni.setStorageSync('AccountLogin', this.loginForm)
						uni.switchTab({
							url: '/pages/user'
						})
						this.$store.dispatch('getTheme')
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.normal-login-container {
		width: 100%;
		height: 100vh;
		background-color: #ffffff;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 15%;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			margin-top: 15%;
			width: 80%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}
			}

			.login-btn {
				margin-top: 40px;
				height: 45px;
				border-radius: 50px;
				background-color: #0081ff;
				color: white;
			}
		}
	}

	.btnHover {
		transform: translate(1px, 1px);
	}
</style>